using FixMath.NET;
using GameLogic.Data;
using GameProtocol;
using TEngine;
using UnityEngine;

namespace GameLogic
{
    [Update]
    public class GameDataModule : BehaviourSingleton<GameDataModule>
    {
        public long Time { get { return GameData.Instance.Record.Time; } }
        public override void Update()
        {
            base.Update();
            if (Input.GetKeyDown(KeyCode.F5))
            {
                GameData.Instance.Save();
                Debug.Log("保存完成");
            }
            if (Input.GetKeyDown(KeyCode.F6))
            {
                GameDataModule.Instance.AddItem(200000, 1000);
                GameDataModule.Instance.AddItem(200001, 500);
                GameDataModule.Instance.AddItem(200002, 500);
                GameDataModule.Instance.AddItem(200003, 500);
                Debug.Log("添加成功");
            }
            GameData.Instance.Record.Time += (int)(UnityEngine.Time.fixedDeltaTime * 1000);
        }
        public int GetItemID(int idx)
        {
            return GameData.Instance.Record.Items[idx].Id;
        }
        public int GetItemCount(int idx)
        {
            return GameData.Instance.Record.Items[idx].Count;
        }
        public int GetItemListLength()
        {
            return GameData.Instance.Record.Items.Count;
        }

        /// <summary>
        /// 获取武器类型的计数
        /// </summary>
        /// <returns></returns>
        public int GetWeaponListLength()
        {
            int weaponCount = 0;
            var itemCount = GetItemListLength();
            for (var i =0; i< itemCount; ++i)
            {
                var itemID = GetItemID(i);
                var type = ItemModule.Instance.GetItemType(itemID);
                if(type == GameConfig.item.EType.Weapon)
                {
                    weaponCount++;
                }
            }
            return weaponCount;
        }
        public void AddShip(ShipData data)
        {
            GameData.Instance.Record.Ships.Add(data);
        }
        public void SetDriveShipIndex(int idx)
        {
             GameData.Instance.Record.DriveShipIndex = idx;
        }
        public int GetDriveShipIndex()
        {
            return GameData.Instance.Record.DriveShipIndex;
        }
        public ShipData GetDriveShipData()
        {
            return GetShipData(GameData.Instance.Record.DriveShipIndex);
        }
        public void AddItem(int itemID, int itemCount)
        {
            if (itemID <= 0)
                return;
            int idx = FindItemIdx(itemID);
            if(idx > 0)
            {
                GameData.Instance.Record.Items[idx].Count += itemCount;
            }
            else
            {
                var itemData = new ItemData();
                itemData.Id = itemID;
                itemData.Count = itemCount;
                GameData.Instance.Record.Items.Add(itemData);
            }
        }
        public void RemoveItem(int itemID, int itemCount)
        {
            int idx = FindItemIdx(itemID);
            if (idx > 0)
            {
                GameData.Instance.Record.Items[idx].Count -= itemCount;
            }
            else
            {
                Debug.LogError("you are trying remove a item whitch you have not: " + itemID);
            }

        }
        public int FindItemIdx(int itemID)
        {
            for (int i=0; i<GameData.Instance.Record.Items.Count; ++i )
            {
                var foundItemID = GetItemID(i);
                if(foundItemID == itemID)
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// 获取武器类型的计数
        /// </summary>
        /// <returns></returns>
        public int GetWeaponID(int idx)
        {
            int weaponCount = 0;
            var itemCount = GetItemListLength();
            for (var i = 0; i < itemCount; ++i)
            {
                var itemID = GetItemID(i);
                var type = ItemModule.Instance.GetItemType(itemID);
                if (type == GameConfig.item.EType.Weapon )
                {
                    if(weaponCount == idx)
                    {
                        return itemID;
                    }
                    weaponCount++;
                }
            }
            return 0;
        }

        public ShipData GetShipData(int idx)
        {
            return GameData.Instance.Record.Ships[idx];
        }
        public int GetNumShipData()
        {
            return GameData.Instance.Record.Ships.Count;
        }
    }
}
